package com.henu.yizhiya.util;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.henu.R;
import com.henu.yizhiya.db.DBManager;
import com.henu.yizhiya.db.GetRoomsActivity;
import com.henu.yizhiya.db.RoomDB;
import com.henu.yizhiya.entity.Course;
import com.henu.yizhiya.entity.Room;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SQLQueryListener;

import static com.henu.R.id.lv_rooms;

public class BuildingActivity extends AppCompatActivity {
    private DBManager manager;
    private ArrayList<RoomDB> room_list;
    private BuildListAdapter adapter;
    private ListView lv_build;
    private ImageButton build_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_building);

        build_return = (ImageButton) findViewById(R.id.build_return);


        //加载rooms适配器
        adapter = new BuildListAdapter(this);
        lv_build = (ListView) findViewById(R.id.lv_build);
        lv_build.setAdapter(adapter);


        //引入Bmob
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        manager = DBManager.getInstance(this);

        if (!get_state()) {
            BmobQuery<Room> query = new BmobQuery<>();
            query.setLimit(1000);
            query.findObjects(new FindListener<Room>() {
                @Override
                public void done(List<Room> list, BmobException e) {
                    if (e == null) {
                        System.out.println("------"+list.size());
                        if (list.size() != 0) {
                            for (Room r : list) {
                                String num = r.getR_num().charAt(0)+"";
                                manager.insert(r,num);
                            }
                        }
                        save_state();
                        showBuild();
                        Toast.makeText(BuildingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(BuildingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }else {
            showBuild();
        }

        lv_build.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<RoomDB> list_r=adapter.getItem(position);
                Intent intent = new Intent(BuildingActivity.this,GetRoomsActivity.class);
                intent.putExtra("room_list", (Serializable) list_r);
                startActivity(intent);
            }
        });


        build_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public void showBuild(){
        room_list = manager.searchAll();
        adapter.addAllData(room_list);
        adapter.notifyDataSetChanged();
    }


    //保存状态
    public void save_state() {
        SharedPreferences sp = getSharedPreferences("text", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("get_build", true);
        editor.commit();

    }
    //获取状态
    public boolean get_state() {
        SharedPreferences sp = getSharedPreferences("text", MODE_PRIVATE);
        boolean b = sp.getBoolean("get_build", false);
        if (b) {
            return true;
        } else {
            return false;
        }
    }
}
